#!/bin/bash

function clean_object_files()
{
    local makeBin="$1"
    local homeworkRoot="$2"
    
    # run the clean command
    $makeBin -C $homeworkRoot clean > /dev/null
}

function run_compile()
{
    local timeBin="$1"
    local makeBin="$2"
    local homeworkRoot="$3"
    local compileFlag="$4"
    local compileOptionName="OPT_FLAGS"
    
    # running the compile command
    $timeBin $makeBin -C $homeworkRoot EXE="vpr_$(echo $compileFlag | sed "s, ,,g").exe" OPT_FLAGS="$compileFlag" 2>&1
}

# script to gather data for part 3.3 of the homework

TIME_BIN="/usr/bin/time"
MAKE_BIN="/usr/bin/make"
HW1_DIR="/homes/a/abbasmus/courses/454ece/hw1"

COMPILE_OPT_FLAGS=( "-g -pg" "-g -fprofile-arcs -ftest-coverage" "-g" "-O2" "-O3" "-Os" )

pushd $HW1_DIR

echo "COMPILE FLAG" "AVERAGE TIME" "SPEED UP"

for compileFlag in "${COMPILE_OPT_FLAGS[@]}"; do
    echo -n "$compileFlag" ""

    #run the compile 5 times
    clean_object_files "$MAKE_BIN" "$HW1_DIR"
    run_compile "$TIME_BIN" "$MAKE_BIN" "$HW1_DIR" "$compileFlag" > /dev/null
done

clean_object_files "$MAKE_BIN" "$HW1_DIR"

popd
